package com.yunst.net.handle;

import com.yunst.execute.impl.ClickExecute;
import com.yunst.execute.impl.ExecuteTask;
import com.yunst.net.message.EventEnum;
import com.yunst.net.message.EventMessage;
import com.yunst.net.message.Message;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.awt.*;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.nio.charset.CharsetEncoder;
import java.nio.charset.StandardCharsets;

/**
 * @author yunst
 * @date 2022/12/31
 */
public class EventHandle extends SimpleChannelInboundHandler<EventMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, EventMessage eventMessage) throws Exception {
        System.out.println("事件消息内容：" + new String(eventMessage.getContent()));
        messageHandle(channelHandlerContext, eventMessage);
    }

    public void messageHandle(ChannelHandlerContext channelHandlerContext, EventMessage eventMessage) {
        byte[] content = eventMessage.getContent();
        String value = new String(content, StandardCharsets.UTF_8);
        String[] split = value.split(":");
        Integer event = Integer.valueOf(split[0]);
        switch (event) {
            case 0:
                new ExecuteTask(channelHandlerContext).start();
                break;
            case 1:
                ClickExecute.click(new Point(Integer.valueOf(split[1]), Integer.valueOf(split[2])));
                break;

        }

    }
}
